package com.qding.park.manager.timer.schedule.job;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobKey;
import org.springframework.stereotype.Service;

import com.baidu.disconf.client.common.annotations.DisconfUpdateService;
import com.baidu.disconf.client.common.update.IDisconfUpdate;
import com.qding.park.common.core.cache.service.CacheServiceFacade;
import com.qding.park.common.core.constant.CacheKeyThree;
import com.qding.park.common.core.redis.SerializeUtils;
import com.qding.park.manager.pojo.TbReserve;
import com.qding.park.manager.pojo.queue.QueueMsg;
import com.qding.park.manager.pojo.queue.QueueTypeEnum;
import com.qding.park.manager.timer.ParkTimerApplication;
import com.qding.park.manager.timer.conf.TaskConfig;

/**
 * @ClassName ReserveCheckJob
 * @Description TODO(预定单超时检查任务:分为两种超时 1超时2延期超时)
 * @author Administrator
 * @Date 2016年9月15日 下午3:53:58
 * @version 1.0.0
 */
@Service
@DisconfUpdateService( classes={TaskConfig.class},itemKeys = { "task.ReserveCheckJob.name" ,"task.ReserveCheckJob.taskJobClass","task.ReserveCheckJob.cronTime"})
public class ReserveCheckJob extends AbstractJob implements IDisconfUpdate{
	private static final Log _log = LogFactory.getLog(ReserveCheckJob.class);

	@Override
	public void execJob(JobExecutionContext context) {
		JobKey jobKey = context.getJobDetail().getKey();
//		_log.info("ReserveCheckJob starting");
		reserver_timeout();
//		_log.info("ReserveCheckJob ended");
//		reserver_delay_timeout();
//		// 得到redis client
//		// JedisClient jedisClient =
//		// ParkTimerApplication.ctxUtil.getApplicationContext().getBean(JedisClient.class);
//
//		CacheServiceFacade cacheServiceFacade = ParkTimerApplication.ctxUtil.getApplicationContext()
//				.getBean(CacheServiceFacade.class);
//
//		// 获取预定单的 监控列表
//		Map<String, String> mset = jedisClient.hgetAll(CacheKeyThree.MONITOR_RESERVE_KEY);
//
//		if (MapUtils.isNotEmpty(mset)) {
//			for (Map.Entry<String, String> entry : mset.entrySet()) {
//				String key = entry.getValue();
//				String value = entry.getValue();
//
//				TbReserve tbReserve = SerializeUtils.unSerialize(value, TbReserve.class);
//				Date lastTime = tbReserve.getLastTime();
//				Date now = new Date();
//				Date endTime = tbReserve.getEndTime();
//				if (now.getTime() > endTime.getTime()) {
//					// 超时
//					QueueMsg msg = new QueueMsg();
//
//					msg.setEventType(QueueTypeEnum.RESERVE_TIMEOUT.getType());
//					TbReserve data = new TbReserve();
//					data.setId(tbReserve.getId());
//					msg.setData(data);
//
//					String msgStr = SerializeUtils.serialize(msg);
//
//					// 预定单超时 消息
//					jedisClient.lpush(CacheKeyThree.QUEUE_ALL, msgStr);
//				} else if (now.getTime() > lastTime.getTime()) {
//					// 延期超时
//					QueueMsg msg = new QueueMsg();
//					msg.setEventType(QueueTypeEnum.RESERVE_DELAY_TIMEOUT.getType());
//					TbReserve data = new TbReserve();
//					data.setId(tbReserve.getId());
//					msg.setData(data);
//					String msgStr = SerializeUtils.serialize(msg);
//					// 预定单超时 消息
//					jedisClient.lpush(CacheKeyThree.QUEUE_ALL, msgStr);
//				}
//			}
//		}

		_log.info("预定单超时检查任务: " + jobKey + " executing at " + new Date());

	}

	private void reserver_timeout() {
		CacheServiceFacade cacheServiceFacade = ParkTimerApplication.ctxUtil.getApplicationContext()
				.getBean(CacheServiceFacade.class);

		List<TbReserve> reserveList = cacheServiceFacade.reserve_timeout_monitor_list();

		for (TbReserve tbReserve : reserveList) {
			Date now = new Date();
			Date endTime = tbReserve.getEndTime();

			if (now.getTime() > endTime.getTime()) {
				// // 超时
				cacheServiceFacade.reserver_timeout_queue_push(tbReserve);
				//删除监控
				cacheServiceFacade.reserve_monitor_delete(tbReserve.getId());
			}
		}

	}
	
	private void reserver_delay_timeout() {
		CacheServiceFacade cacheServiceFacade = ParkTimerApplication.ctxUtil.getApplicationContext()
				.getBean(CacheServiceFacade.class);

		List<TbReserve> reserveList = cacheServiceFacade.reserve_delay_timeout_monitor_list();

		for (TbReserve tbReserve : reserveList) {
			Date lastTime = tbReserve.getLastTime();
			Date now = new Date();

			if (now.getTime() > lastTime.getTime()) {
				// // 延期超时
				cacheServiceFacade.reserver_delay_timeout_queue_push(tbReserve);
			}
		}

	}

	@Override
	public void reload() throws Exception {
		_log.info(TaskConfig.getTask_ReserveCheckJob_name());
		String taskName=TaskConfig.getTask_ReserveCheckJob_name();
		String cronTime=TaskConfig.getTask_ReserveCheckJob_cronTime();
		super.reloadTask(taskName,cronTime);
	}
}
